<?php

namespace App\Controller;

use App\Response\Response;
use App\Services\Admin\AdminAuthService as AuthService;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Validation\ValidatorFactory;

/**
 * 用户的登录注册系列控制器
 */
class AuthController extends AbstractController
{
    /**
     * @var ValidatorFactory
     */
    public $validator;

    /**
     * @var AuthService
     */
    public $authService;

    public function __construct(ValidatorFactory $validator,AuthService $authService)
    {
        parent::__construct();
        $this->validator = $validator;
        $this->authService = $authService;
    }

    /**
     * 用户登录
     * @param RequestInterface $request
     * @return Response
     */
    public function login(RequestInterface $request)
    {
        $validator = $this->validator->make($params = $request->all(),[
            "email" => "required|email",
            "passwd" => "required|digits_between:6,12",
        ]);
        if($validator->fails()){
            return new Response(1,"参数校验失败",["errors"=>$validator->getMessageBag()->all()]);
        }
        //api和服务的字段可能不一样，所以这里重新组织参数
        $response = $this->authService->login(["email" => $params["email"], "passwd" => $params["passwd"]]);
        return $response->getResponse();
    }
}